<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    @import "link.html"

    <style>
        html, body {
            height: 100%;
        }
    </style>
</head>

<body>
<div id="box" style="height: 100%;padding: 2px;">
    <div id="dg"></div>
    <div id="tb" style="padding: 5px">
        <div>
            <form id="formObj">
                <div class="inputs">
                    <label class="label-item">
                        <span>下达状态：</span>
                        <select class="easyui-combobox" panelHeight="auto" style="width:100px" name="state">
                            <option value="1">未下达</option>
                            <option value="2">已下达</option>
                        </select>
                    </label>
                    <label class="label-item">
                        <span>预算单位名称：</span>
                        <select id="ser-ipt" style="width:100px" name="ys_dept"></select>
                    </label>
                    <label class="label-item">
                        <span>年度：</span>
                        <input class="easyui-datebox" style="width:120px" name="year">
                    </label>
                    <label class="label-item">
                        <span>项目名称：</span>
                        <input class="easyui-textbox" name="pro_name">
                    </label>
                </div>
                <div class="btns">
                    <a href="#" class="easyui-linkbutton" iconCls="fa fa-search">查询</a>
                    <a href="#" class="easyui-linkbutton" iconCls="fa fa-refresh">重置</a>
                </div>
            </form>
        </div>
        <div>
            <a id="add" href="#" class="easyui-linkbutton" data-options="iconCls:'fa fa-plus'">新增</a>
            <a id="edit" href="#" class="easyui-linkbutton" data-options="iconCls:'fa fa-edit'">编辑</a>
        </div>
    </div>
</div>
<script>
  $(function () {
    $('#ser-ipt').combobox({
      valueField: 'id',
      textField: 'text',
      panelHeight:null,
      data:[{
        "id": 11,
        "text": "党政办"
      }, {
        "id": 12,
        "text": "社会事务办"
      }, {
        "id": 13,
        "text": "经发办"
      }, {
        "id": 14,
        "text": "综治办"
      }, {
        "id": 15,
        "text": "城建办"
      }, {
        "id": 16,
        "text": "综合指挥室"
      }, {
        "id": 17,
        "text": "城管中队"
      }, {
        "id": 18,
        "text": "环卫站"
      }, {
        "id": 19,
        "text": "消防队"
      }, {
        "id": 20,
        "text": "交通协管"
      }],
      filter:function (q,row) {
        var opts = $(this).combobox('options');
        return row[opts.textField].indexOf(q) >= 0;
      }
    });

    var rowindex=0;
    $('#dg').datagrid({
      url: '../assets/json/datagrid_data1.json',
      idField: 'itemid',
      method:'get',
      fit: true,
      loadMsg: '数据加载中...',
      pageSize: 10,
      pagination: true,
      pageList: [10, 20, 30],
      rownumbers: true,//开启行号
      singleSelect: true,
      remoteSort:false,
      toolbar: "#tb",
      columns: [[{
          field: 'itemid',
          title: 'itemid',
          width: 100
        }, {
          field: 'productid',
          title: 'productid',
          width: 100
        }, {
          field: 'productname',
          title: 'productname',
          width: 100
        }, {
          field: 'unitcost',
          title: 'unitcost',
          width: 100,
          align: 'right',
          sortable: true,
          sorter:function(a,b){
            return a-b;
          }
        }, {
          field: 'status',
          title: 'status',
          width: 100,
          align: 'right'
        }, {
          field: 'listprice',
          title: 'listprice',
          width: 100
        }, {
          field: 'attr1',
          title: 'attr1',
          width: 100
        }
      ]],
      onSortColumn:function (s,o) {
        var params=$('#formObj').serialize()+'&field='+s+'&order='+o;
        function formToJson(data) {
          data=data.replace(/&/g,"\",\"");
          data=data.replace(/=/g,"\":\"");
          data="{\""+data+"\"}";
          return data;
        }
        $('#dg').datagrid('load',{
          queryParams:formToJson(params)
        });
      },
      onCheck:function (rowIndex,rowData) {
        rowindex=rowIndex+1;
        console.log();
      },
      onUnCheck:function (rowIndex,rowData) {
        rowindex=0;
      }
    });

    function reloadDG() {
      $('#dg').datagrid('reload');
      console.log('reload');
    }
    window.reloadDG=reloadDG;

    $('#add').on('click', function () {
      parent.iframeShow('新增', '../y-demo/datagrid-add.html', [], {
        btn: ['确定', '取消'],
        yes: function (index, dom) {
          var ifrBody = dom.find('iframe').contents();
          ifrBody.find('#submit-btn').trigger('click');
        }
      })
    });

    $('#edit').on('click', function () {
      if(rowindex){
        var ifrBody=null;
        parent.iframeShow('编辑', '../y-demo/datagrid-add.html', [], {
          btn: ['确定', '取消'],
          success:function (dom, index) {
            ifrBody=dom.find('iframe').contents();
            // 获取子页面，并执行子页面的方法
            var win=parent.window[dom.find('iframe')[0]['name']];
            win.setVal({
              name: '领衔主演',
              email: 'mymail@gmail.com',
              pro_name:'我是名称'
            });
          },
          yes: function (index, dom) {
            ifrBody.find('#submit-btn').trigger('click');
          }
        })
      }else {
        parent.errorMsg('请选择一行数据');
      }
    });


  });
</script>
</body>
</html>